#!/bin/bash
#(c) Copyright Barry Kauler 2009.
# grab binary ubuntu/debian/slackware packages and build puppy packages.
# each binary package is expanded in sandbox2/ then copied as split pkgs
# to packages-$DISTRO_FILE_PREFIX/, into the "generic name". Note that one generic
# name can have one or more binary pkgs.
# Note, DISTRO_FILE_PREFIX is specified in DISTRO_SPECS file.
#110820 support/findpkgs is new script to find all pkgs to be used in Puppy build. Major changes!
#110830 fix if 3rd field specifies multiple pet pkgs.
#111204 do not execute post-install script if FIXUPHACK has DISABLE_POST_INSTALL_SCRIPT=yes. (this existed before, but was broken)
#121102 file DISTRO_SPECS has new variable DISTRO_DB_SUBNAME. ex: for 14.0-based slacko, DISTRO_DB_SUBNAME=slacko14
#
#some information about template-packages/GENERICNAME:
#  copy all non-zero files to target (overwrite existing files)
#As a last resort, if target pkg is wrong, a file 'FIXUPHACK' is a script that can be at top dir
#  in template. It executes in target, with current-dir set to where FIXUPHACK is located. (ex: perl_tiny).
#

#set -x

MWD=$(pwd)

export LANG=C #faster.

case $1 in
	-h*)
		echo " syntax:
	$0 <pkg> - process specific pkg or 'generic name'
	$0 <commands>

 commands:

	-all: force processing all packages
	-changed: [currently broken.. apparently]
	-downloaded: process all packages listed in status/download_list as made by 1download
"
	exit
	;;
	-all) COMMANDLINE_CHOICE=ALL_PACKAGES ;;
	-changed) COMMANDLINE_CHOICE=CHANGED_ONLY ;;
	-downloaded)
		#create all packages listed in status/download_list as made by 1download
		if [ ! -f ./status/download_list ] ; then
			echo "/status/download_list cannot be found - exiting"
			exit -1
		fi
		for pkg in `sort -u ./status/download_list | cut -d : -f 2`
		do
			[ "$pkg" != "" ] && $0 $pkg
		done
		exit 0
		;;
esac

. ./_00build.conf
. ./_00func
. ./DISTRO_SPECS #has DISTRO_BINARY_COMPAT, DISTRO_COMPAT_VERSION
[ ! "$DISTRO_DB_SUBNAME" ] && DISTRO_DB_SUBNAME="$DISTRO_COMPAT_VERSION" #121102 fallback if DISTRO_DB_SUBNAME not defined in file DISTRO_SPECS.
source_compat_repos  # ./DISTRO_COMPAT_REPOS-
source_pkgs_specs    #sets DPSFILE / has FALLBACKS_COMPAT_VERSIONS
. ./DISTRO_PET_REPOS #has PET_REPOS, PACKAGELISTS_PET_ORDER
source_woofmergevars # ./WOOFMERGEVARS
set_archdir_var      #ex: ARCHDIR=x86_64-linux-gnu
set_binaries_var     #ex: BINARIES=tgz_txz-14.2 BINARIES=deb-xenial
create_local_repos_dirs

#./DISTRO_SPECS
export DISTRO_BINARY_COMPAT #120517 i want this in FIXUPHACK scripts.
export DISTRO_COMPAT_VERSION DISTRO_FILE_PREFIX DISTRO_TARGETARCH
#./WOOFMERGEVARS
export WOOF_HOSTARCH WOOF_TARGETARCH WOOF_COMPATDISTRO WOOF_COMPATVERSION
export BINARIES #ex: deb-xenial

# $PLIBDIRS
PLIBDIRS="lib usr/lib"
if [ "$DISTRO_BINARY_COMPAT" = "slackware64" ] ; then
	PLIBDIRS="lib64 usr/lib64"
fi
#if [ "$ARCHDIR" ] ; then
#	PLIBDIRS="$PLIBDIRS lib/${ARCHDIR} usr/lib/${ARCHDIR}"
#fi
export PLIBDIRS

PLIB=lib
[ "$DISTRO_BINARY_COMPAT" = "slackware64" ] && PLIB=lib64
export PLIB

CURRDIR="`pwd`"

#PKGS_SPECS_TABLE table fomat:
#will pkg be in woof.
#    Generic name for pkg. Note: PET packages, if exist, use this name + version#.
#            Comma-separated list of ubuntu/debian part-names for pkg(s). '-' prefix, exclude.
#            Empty field, then use PET pkg.
#                    How the package will get split up in woof (optional redirection '>' operator).
#                    Missing field, it goes into exe. Can also redirect >null, means dump it.
#yes|abiword|iceword_|exe,dev,doc,nls

#remove comments from PKGS_SPECS_TABLE... make sure '|' on end... get rid of old |+udev,+whatever on end...
PKGS_SPECS_TABLE="`echo "$PKGS_SPECS_TABLE" | grep -v '^#' | grep -v '^$' | tr '\t' ' ' | tr -s ' ' | tr '+' '&' | sed -e 's%|&.*%%' | tr '&' '+' | sed -e 's% #.*%%' -e 's% $%%' -e 's%$%|%' -e 's%||$%|%'`"

run_findpkgs #find all packages that will be used in the Puppy build...

if [ -z "$COMMANDLINE_CHOICE" ] ; then
	COMMANDLINE_CHOICE=""
	[ $1 ] && COMMANDLINE_CHOICE="$1"
fi

mkdir -p /tmp/2createpackages_tmp

#091211 to save time rebuild only changed pkgs COMMANDLINE_CHOICE="CHANGED_ONLY"...
mkdir -p status
if [ "$COMMANDLINE_CHOICE" = "ALL_PACKAGES" ] ; then
  #this file will keep growing, so reset it...
  echo -n "" > status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
else
  touch status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
  sort -u status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} > status/z2c
  mv -f status/z2c status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
fi

mkdir -p packages-${DISTRO_FILE_PREFIX}
while [ 1 ];do
 if [ "$COMMANDLINE_CHOICE" = "CHANGED_ONLY" ];then #091211
  CHOICE_PKGS_SPECS_TABLE="$PKGS_SPECS_TABLE"
  break
 fi
 if [ "$COMMANDLINE_CHOICE" = "" ];then
  echo
  echo "Press ENTER only to build all pkgs,"
  echo -n " or type generic name of one pkg: "
  read USERINPUT
 else
  if [ "$COMMANDLINE_CHOICE" = "ALL_PACKAGES" ];then #see woof_gui_tabs.
   USERINPUT=""
  else
   USERINPUT="$COMMANDLINE_CHOICE"
  fi
 fi
 if [ "$USERINPUT" = "" ];then
  rm -rf packages-${DISTRO_FILE_PREFIX}
  mkdir packages-${DISTRO_FILE_PREFIX}
  CHOICE_PKGS_SPECS_TABLE="$PKGS_SPECS_TABLE"
  rm -rf sandbox2c 2>/dev/null
 else
  CHOICE_PKGS_SPECS_TABLE="`echo "$PKGS_SPECS_TABLE" | grep "^yes|${USERINPUT}|"`"
  if [ "$CHOICE_PKGS_SPECS_TABLE" = "" ];then
   echo "ERROR: Generic-name must be from 2nd field of variable PKGS_SPECS_TABLE"
   echo "in file ${DPSFILE}."
   echo "There must also be a 'yes' in the first field."
  fi
 fi
 break
done

#121120 further down, wants this in the host system...
[ -f rootfs-skeleton/usr/local/petget/categories.dat ] && cp -f rootfs-skeleton/usr/local/petget/categories.dat /usr/local/petget/

############################################################################
#read through PKGS_SPECS_TABLE, find matching pkg(s) in 'packages-${BINARIES}',
#process and create raw woof packages in 'packages-${DISTRO_FILE_PREFIX}'...

STRIP_BIN="`which strip`"

echo -n "" > /tmp/2createpackages-redo #091211
rm -f ERROR-2CREATEPACKAGES 2>/dev/null

pre_process_pkg() {
  # before processing the whole package, check if there is a _PRE file
  # which contains code to fix/trim dowm stuff to speed up processing...
  if [ -f packages-templates/${GENERICNAME}/_PRE ] ; then
    echo " executing packages-templates/${GENERICNAME}/_PRE"
    ( cd sandbox2 ; sh ../packages-templates/${GENERICNAME}/_PRE )
  elif [ -f packages-templates/${GENERICNAME}_PRE ] ; then
    echo " executing packages-templates/${GENERICNAME}_PRE"
    ( cd sandbox2 ; sh ../packages-templates/${GENERICNAME}_PRE )
  fi
}

for ONEPKGSPEC in $CHOICE_PKGS_SPECS_TABLE
do

 [ "$ONEPKGSPEC" = "" ] && continue

 IFS="|" read -r YESNO GENERICNAME BINARYPARTNAMES SPLITRULE PKGLOCFLD ETC <<< "$ONEPKGSPEC"
 [ "$YESNO" = "no" ] && continue
 BINARYPARTNAMES="${BINARYPARTNAMES//,/ }" #convert "," to " "

 MISSPOSTPROC='no' #091211
 echo
 echo "Processing ${GENERICNAME}"
 REMOVEDGENFLAG=0 #091211

 #processing pkgs that will be combined into ${GENERICNAME}...
 gennamePTN=":${GENERICNAME}:" #a pkg may be shared by multiple generic names, ex: :gcc_lib::gcc_dev:|compat|... (ex from Slacko).
 FNDIT="`grep "$gennamePTN" status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}`"
 if [ "$FNDIT" = "" ];then
  ( echo
   echo "ERROR: '${GENERICNAME}' package does not exist."
   echo "You will need to find a matching package and place in packages-pet,"
   echo "or packages-${BINARIES} as appropriate."
   echo "Do it, then rerun this script and choose to build ${GENERICNAME}." ) >> ERROR-2CREATEPACKAGES
   continue
 fi

 PKG_DIR=${PWD}/packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}
 [ -d ${PKG_DIR} ] && rm -rf ${PKG_DIR}
 [ -d ${PKG_DIR}_DEV ] && rm -rf ${PKG_DIR}_DEV
 [ -d ${PKG_DIR}_DOC ] && rm -rf ${PKG_DIR}_DOC
 [ -d ${PKG_DIR}_NLS ] && rm -rf ${PKG_DIR}_NLS
 [ -d ${PKG_DIR}_NULL ] && rm -rf ${PKG_DIR}_NULL
 [ -d sandbox2c/${GENERICNAME} ] && rm -rf sandbox2c/${GENERICNAME}
 
 # rootfs-packages override pet pkgs
 if [ -d ${PWD}/rootfs-packages/${GENERICNAME} ] ; then
   echo " * rootfs-packages/${GENERICNAME} overrides pet pkg"
   if [ -f ${PWD}/rootfs-packages/${GENERICNAME}/pet.specs ] ; then
      IFS="|" read a b < ${PWD}/rootfs-packages/${GENERICNAME}/pet.specs
      echo " processing $a"
   fi
   cp -a ${PWD}/rootfs-packages/${GENERICNAME} ${PWD}/packages-${DISTRO_FILE_PREFIX}
   continue
 fi
 
 #inject $FNDIT at end of do-done, runs faster...
 while read ONEDBENTRY
 do
  #120812 category now allows optional subcategory...
  #ONEDBENTRY=:GENERICNAME:|PETorCOMPAT|DBFILE|pkgname|nameonly|version|pkgrelease|category[;subcategory]|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
  #...pkgname onwards is the full db entry for a package.
  IFS="|" read -r F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 ETC <<< "$ONEDBENTRY"
  PETorCOMPAT="$F2" #ex: compat
  if [ "$PETorCOMPAT" != "pet" -a "$PETorCOMPAT" != "compat" ];then
   ( echo
    echo "Syntax ERROR in status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}:"
    echo " $ONEDBENTRY"
    echo "Ignoring this line." ) >> ERROR-2CREATEPACKAGES
    continue
  fi
  DBFILE="$F3"           #ex: Packages-puppy-5-official
  DB_pkgname="$F4"       #ex: abiword-2.3.4-w5
  DB_fullfilename="$F11" #ex: abiword-2.3.4-w5.pet
  petFLG=""
  case "$DB_fullfilename" in *.pet) petFLG=pet ;; esac

  #PKGS_SPECS_TABLE may specify redirections...
  EXE="" ; DEV="" ; DOC="" ; NLS=""
  #exe>dev,dev>exe,etc
  #exe,dev,doc>exe,nls>null
  for TOK in ${SPLITRULE//,/ } ; do #convert "," to " "
	case $TOK in
		exe*) EXE="${TOK#*>}" ; EXE="_${EXE^^}" ;;
		dev*) DEV="${TOK#*>}" ; DEV="_${DEV^^}" ;;
		doc*) DOC="${TOK#*>}" ; DOC="_${DOC^^}" ;;
		nls*) NLS="${TOK#*>}" ; NLS="_${NLS^^}" ;;
	esac
  done
  #the "packagename_EXE" is just named "packagename"...
  [ "$EXE" = "_EXE" ] && EXE=""
  [ "$DEV" = "_EXE" ] && DEV=""
  [ "$DOC" = "_EXE" ] && DOC=""
  [ "$NLS" = "_EXE" ] && NLS=""

  #---- check if need to stop here
  if [ "$EXE" = "_NULL" -a "$DEV" = "_NULL" -a "$NLS" = "_NULL" ] ; then
    # don't want anything fromt this pkg... a 'fake install'
    mkdir -p "packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${EXE%_NULL}"
    continue
  fi
  case "$DB_fullfilename" in *"-dev_"*|*"_DEV"*)
    if [ "$DEV" = "_NULL" ] ; then
      mkdir -p "packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${EXE%_NULL}_DEV"
      continue
    fi ;;
  esac
  case "$DB_fullfilename" in *"-doc_"*|*"_DOC"*)
    [ "$DOC" = "_NULL" ] && continue ;;
  esac
  case "$DB_fullfilename" in *"_NLS"*) continue ;; esac
  #--

  #------------------------------------------------------------------------
  PKG_DIR=${PWD}/packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}
  EXE_DIR=${PWD}/packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${EXE%_NULL}
  DEV_DIR=${PWD}/packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${DEV}
  DOC_DIR=${PWD}/packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${DOC}
  NLS_DIR=${PWD}/packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${NLS}
  #------------------------------------------------------------------------

  if [ "$petFLG" = "pet" ];then #do not test PETorCOMPAT only.
   PETPKG=${DB_fullfilename%.pet} #remove trailing ".pet"
   PETSUB="pet" #fix finding compat PETs in wary.
   [ "$PETorCOMPAT" = "compat" ] && PETSUB="$BINARIES" #110823

   if [ "$COMMANDLINE_CHOICE" = "CHANGED_ONLY" ];then
    if [ "`grep "|${PETPKG}.pet\$" status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}`" != "" ];then
     echo " ...${PETPKG}.pet already processed"
     continue
    fi
    if [ $REMOVEDGENFLAG -eq 0 ];then
     #get rid of old entries...
     grep -v "^${GENERICNAME}|" status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} > /tmp/2createpackages-hbdd
     mv -f /tmp/2createpackages-hbdd status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
     REMOVEDGENFLAG=1
    fi
   fi
   echo "${GENERICNAME}|${PETPKG}.pet" >> status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
   #now expand $PETPKG.pet in packages-${DISTRO_FILE_PREFIX}...
   echo " processing ${PETPKG}.pet"
   #PKGS_SPECS_TABLE may specify redirections...
   case "$DB_pkgname" in
     *"_DEV"*) xPETTYPE="$DEV" ;;
     *"_DOC"*) xPETTYPE="$DOC" ;;
     *"_NLS"*) xPETTYPE="$NLS" ;;
     *)        xPETTYPE="$EXE" ;;
   esac
   xxPETTYPE=${xPETTYPE}
   rm -rf sandbox2e 2>/dev/null #110830
   mkdir sandbox2e #110830
   if [ -f packages-${PETSUB}/${PETPKG}.pet ];then #110823
    cp -f packages-${PETSUB}/${PETPKG}.pet sandbox2e/ #110830
    cd sandbox2e
    pet2tgz ${PETPKG}.pet
    tar -a -x -f ${PETPKG}.tar.[gx]z
    mkdir -p ../packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${xxPETTYPE} #110830
    cp -a -f --remove-destination $PETPKG/* ../packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${xxPETTYPE}/ #110830
    sync
    rm -f ${PETPKG}.tar.[gx]z
    rm -rf $PETPKG
    cd $MWD
    if [ -d packages-templates/${PETPKG}.pet ] ; then
      echo " postprocessing with packages-templates/${PETPKG}.pet"
      cp -a --remove-destination packages-templates/${PETPKG}.pet/* packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${xxPETTYPE}/
      if [ -f packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${xxPETTYPE}/FIXUPHACK ] ; then
        ( cd packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${xxPETTYPE}/ ; sh ./FIXUPHACK ; rm -f FIXUPHACK )
      fi
    fi
    if [ -d packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${xxPETTYPE}/usr/share/applications/ ] ; then
      sed -i 's| %u|| ; s| %U|| ; s| %f|| ; s| %F||' packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${xxPETTYPE}/usr/share/applications/*.desktop
	fi
   else
	( echo
	echo "ERROR: packages-${PETSUB}/${PETPKG}.pet does not exist."
	echo "(Generic name: ${GENERICNAME})"
	echo "You will need to find this PET package and place in packages-${PETSUB}."
	echo "Do it, then rerun this script and choose to build ${GENERICNAME}." ) >> ERROR-2CREATEPACKAGES
   fi
   rm -rf sandbox2e 2>/dev/null #110830
   continue
  fi 
  
  #--------------------------------
  #anything other than .pet pkgs...
  ONEBINARYPKG="./packages-${BINARIES}/${DB_fullfilename}"
  [ ! -f $ONEBINARYPKG ] && ONEBINARYPKG="./packages-pet/${DB_fullfilename}"
  if [ ! -f $ONEBINARYPKG ];then
   ( echo
    echo "ERROR: ${ONEBINARYPKG} does not exist."
    echo "Please find a package that matches, place it in packages-${BINARIES},"
    echo "then run this script again and choose to build ${GENERICNAME}." ) >> ERROR-2CREATEPACKAGES
   continue 2 #skip this pkg.
  fi
  zPKGNAME="$DB_fullfilename"
  
  #091211 CHANGED_ONLY commandline option...
  if [ "$COMMANDLINE_CHOICE" = "CHANGED_ONLY" ];then
   if [ "`grep "|${zPKGNAME}$" status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}`" != "" ];then
    echo " ...${zPKGNAME} already processed"
    MISSPOSTPROC='yes'
    continue
   fi
   groPATTERN='^'"${GENERICNAME}"'|' #get rid of old entries...
   grep -v "$groPATTERN" status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} > /tmp/2createpackages-hbdd
   mv -f /tmp/2createpackages-hbdd status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
   REMOVEDGENFLAG=1
   #there is a problem, actually need to redo the whole thing afterward...
   echo "${GENERICNAME}" >> /tmp/2createpackages-redo
   continue 2 #process next GENERICNAME
  fi
  echo "${GENERICNAME}|${zPKGNAME}" >> status/2createpackages_history_builds-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}

  if [ -f packages-templates/${GENERICNAME}/FIXUPHACK ] ; then
    FIXUPHACK_FILE="packages-templates/${GENERICNAME}/FIXUPHACK"
  elif [ -f packages-templates/${GENERICNAME}_FIXUPHACK ] ; then
    FIXUPHACK_FILE="packages-templates/${GENERICNAME}_FIXUPHACK"
  else
    FIXUPHACK_FILE=""
  fi
 
  DISABLE_POST_INSTALL_SCRIPT=
  if [ -f "$FIXUPHACK_FILE" ];then
    DISABLE_POST_INSTALL_SCRIPT="`grep '^DISABLE_POST_INSTALL_SCRIPT' "$FIXUPHACK_FILE"`"
  fi

  echo " processing $zPKGNAME"
  PKGNAME="${ONEBINARYPKG##*/}" #basename "$ONEBINARYPKG"
  rm -rf sandbox2
  mkdir sandbox2
  case $DB_fullfilename in
   *.deb) #ubuntu|trisquel|debian|devuan|raspbian etc
    PKGNAME="${PKGNAME%.deb}" #ex: bash_3.2-4ububtu1_i386
    dpkg-deb -x $ONEBINARYPKG sandbox2/
    [ $? -ne 0 ] && { echo "ERROR: failed to unpack $PKGNAME"; continue; }
    pre_process_pkg
   ;;
   *.tgz|*.txz) #slackware*
    PKGNAME="${PKGNAME%.*}" #no extension
    tar --directory=sandbox2/ -ixaf ${ONEBINARYPKG}
    [ $? -ne 0 ] && { echo "ERROR: failed to unpack $PKGNAME"; continue; }
    pre_process_pkg
    if [ -f sandbox2/install/doinst.sh ];then
      # slackware post-install script
      if [ ! "$DISABLE_POST_INSTALL_SCRIPT" ] ; then
		echo " executing (Slackware) install script"
		( cd sandbox2 ; sh ./install/doinst.sh )
		sync
		rm -rf sandbox2/install
      fi
    fi
   ;;
   *.tar.*) #gentoo and others 120515
    PKGNAME="${PKGNAME%.tar.bz2}" #ex: bash-3.2
    tar --directory=sandbox2/ -xaf ${ONEBINARYPKG}
    [ $? -ne 0 ] && { echo "ERROR: failed to unpack $PKGNAME"; continue; }
    pre_process_pkg
   ;;
  esac
  
  # hack for multiarch, move libs to standard locations...
  if [ "$ARCHDIR" ];then
    for BASEDIR in bin lib usr/bin usr/lib usr/include
    do
      if [ -d sandbox2/${BASEDIR}/${ARCHDIR} ] ; then
        cp -a -f --remove-destination sandbox2/${BASEDIR}/${ARCHDIR}/* sandbox2/${BASEDIR}/
        sync
        rm -rf sandbox2/${BASEDIR}/${ARCHDIR}
        ln -s ./ sandbox2/${BASEDIR}/${ARCHDIR}
      fi
    done
  fi

  #if [ -d sandbox2/usr/share/applications ] ; then
  #  sed -i 's| %u||g ; s| %U||g ; s| %f||g ; s| %F||g' sandbox2/usr/share/applications/*.desktop sandbox2/usr/local/share/applications/*.desktop 2>/dev/null
  #fi
  
  #=====================================================================
  #120818 overhauled. Pkg db now has category[;subcategory] (see 0setup), xdg enhanced (see /etc/xdg and /usr/share/desktop-directories), and generic icons for all subcategories.
  #w019 fix .desktop files... 120628 improve...
  DB_ENTRY="`echo -n "$ONEDBENTRY" | cut -f 4-19 -d '|'`" #take GENERICNAME|PETorCOMPAT|DBFILE| off start.
  
  #120818 find out if Categories entry in .desktop is valid, if not use 'category' field in pkg db...
  CATEGORY="`echo -n "$DB_ENTRY" | cut -f 5 -d '|'`" #exs: Document, Document;edit
  [ "$CATEGORY" = "" ] && CATEGORY='BuildingBlock' #paranoid precaution.
  #xCATEGORY and DEFICON will be the fallbacks if Categories entry in .desktop is invalid...
  xCATEGORY="`echo -n "$CATEGORY" | sed -e 's%^%X-%' -e 's%;%-%'`" #ex: X-Document-edit (refer /etc/xdg/menu/*.menu)
  DEFICON="`echo -n "$CATEGORY" | sed -e 's%^%mini-%' -e 's%;%-%'`" #ex: mini-Document-edit (refer /usr/local/lib/X11/mini-icons -- these are in jwm search path)
  case $CATEGORY in
   Calculate)     CATEGORY='Business'             ; xCATEGORY='X-Business'            ; DEFICON='Business.svg'            ;; #Calculate is old name, now Business.
   Develop)       CATEGORY='Utility;development'  ; xCATEGORY='X-Utility-development' ; DEFICON='Utility-development.svg' ;; #maybe an old pkg has this.
   Help)          CATEGORY='Utility;help'         ; xCATEGORY='X-Utility-help'        ; DEFICON='Help.svg'                ;; #maybe an old pkg has this.
   BuildingBlock) CATEGORY='Utility'              ; xCATEGORY='Utility'               ; DEFICON='BuildingBlock.svg'       ;; #unlikely to have a .desktop file.
  esac
  #check validity of Categories= and Icon= entries in .desktop file...
  topCATEGORY="`echo -n "$CATEGORY" | cut -f 1 -d ';'`"
  tPATTERN="^${topCATEGORY} "
  cPATTERN="s%^Categories=.*%Categories=${xCATEGORY}%"
  iPATTERN="s%^Icon=.*%Icon=${DEFICON}%"

  FND_DESKTOP="`find sandbox2/usr/share/applications sandbox2/usr/local/share/applications sandbox2/usr/X11R7/applications -type f -name \*.desktop 2>/dev/null | tr '\n' ' '`"
  if [ "$FND_DESKTOP" ] ; then
    sed -i 's| %u||g ; s| %U||g ; s| %f||g ; s| %F||g' $FND_DESKTOP
  fi

  #121120 if only one .desktop file, first check if a match in /usr/local/petget/categories.dat (see also /usr/local/petget/installpkg.sh)...
  CATDONE='no'
  if [ -f /usr/local/petget/categories.dat ];then #precaution, but it will be there -- yes, have added code above makes sure it is.
   NUMDESKFILE="$(echo -n "$FND_DESKTOP" | wc -w)"
   if [ "$NUMDESKFILE" = "1" ];then
    #to lookup categories.dat, we need to know the generic name of the package, which may be different from pkg name...
    #db entry format: pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
    DBNAMEONLY="$(echo -n "$DB_ENTRY" | cut -f 2 -d '|')"
    DBPATH="$(echo -n "$DB_ENTRY" | cut -f 7 -d '|')"
    DBCOMPILEDDISTRO="$(echo -n "$DB_ENTRY" | cut -f 11 -d '|')"
    case $DBCOMPILEDDISTRO in
     debian|devuan|ubuntu|raspbian) xNAMEONLY=${DBPATH##*/} ;;
     *) xNAMEONLY="$DBNAMEONLY" ;;
    esac
    xnPTN=" ${xNAMEONLY} "
    #130219 categories.dat format changed slightly... ignore case...
    CATVARIABLE="$(grep -i "$xnPTN" /usr/local/petget/categories.dat | grep '^PKGCAT' | head -n 1 | cut -f 1 -d '=' | cut -f 2,3 -d '_' | tr '_' '-')" #ex: PKGCAT_Graphic_camera=" gphoto2 gtkam "
    if [ "$CATVARIABLE" ];then #ex: Graphic-camera
     xCATEGORY="X-${CATVARIABLE}"
     cPATTERN="s%^Categories=.*%Categories=${xCATEGORY}%"
     CATFOUND="yes"
     CATDONE='yes'
    fi
   fi
  fi
  
  for ONEDESKTOP in $FND_DESKTOP
  do
   CATFOUND="no"
   if [ "$CATDONE" = "no" ];then #121120
    for ONEORIGCAT in `cat $ONEDESKTOP | grep '^Categories=' | head -n 1 | cut -f 2 -d '=' | tr ';' ' ' | rev` #search in reverse order.
    do
     #find out if category already valid, if not fallback to xCATEGORY...
     ONEORIGCAT="`echo -n "$ONEORIGCAT" | rev`" #restore rev of one word.
     oocPATTERN=' '"$ONEORIGCAT"' '
     [ "`echo "$PUPHIERARCHY" | tr -s ' ' | grep "$tPATTERN" | cut -f 3 -d ' ' | tr ',' ' ' | sed -e 's%^% %' -e 's%$% %' | grep "$oocPATTERN"`" != "" ] && CATFOUND="yes"
     #got a problem with sylpheed, "Categories=GTK;Network;Email;News;" this displays in both Network and Internet menus...
     if [ "$CATFOUND" = "yes" ];then
      cPATTERN="s%^Categories=.*%Categories=${ONEORIGCAT}%"
      break
     fi
    done
    #121119 above may fail, as DB_category field may not match that in .desktop file, so leave out that $tPATTERN match in $PUPHIERARCHY...
    if [ "$CATFOUND" = "no" ];then
     for ONEORIGCAT in `cat $ONEDESKTOP | grep '^Categories=' | head -n 1 | cut -f 2 -d '=' | tr ';' ' ' | rev` #search in reverse order.
     do
      #find out if category already valid, if not fallback to xCATEGORY...
      ONEORIGCAT="`echo -n "$ONEORIGCAT" | rev`" #restore rev of one word.
      oocPATTERN=' '"$ONEORIGCAT"' '
      [ "`echo "$PUPHIERARCHY" | tr -s ' ' | cut -f 3 -d ' ' | tr ',' ' ' | sed -e 's%^% %' -e 's%$% %' | grep "$oocPATTERN"`" != "" ] && CATFOUND="yes"
      #got a problem with sylpheed, "Categories=GTK;Network;Email;News;" this displays in both Network and Internet menus...
      if [ "$CATFOUND" = "yes" ];then
       cPATTERN="s%^Categories=.*%Categories=${ONEORIGCAT}%"
       break
      fi
     done
    fi
   fi
   
   sed -i -e "$cPATTERN" $ONEDESKTOP #fix Category field.
   #does the icon exist?... fix .desktop... 110821 improve...
   ICON="`grep '^Icon=' $ONEDESKTOP | cut -f 2 -d '='`"
   if [ "$ICON" != "" ];then
    [ -e "sandbox2${ICON}" ] && continue #it may have a hardcoded path.
    [ -e "${ICON}" ] && continue #it may have a hardcoded path, look in running puppy.
    ICONBASE="${ICON##*/}" #basename "$ICON"
    #first search where jwm looks for icons... 111207 check if paths exist...
    FNDICON=""
    [ -d sandbox2/usr/share/pixmaps ] && FNDICON="`find sandbox2/usr/share/pixmaps -maxdepth 1 -name $ICONBASE -o -name $ICONBASE.png -o -name $ICONBASE.xpm -o -name $ICONBASE.jpg -o -name $ICONBASE.jpeg -o -name $ICONBASE.gif -o -name $ICONBASE.svg | grep -i -E 'png$|xpm$|jpg$|jpeg$|gif$|svg$' | head -n 1`"
    [ ! "$FNDICON" ] && [ -d sandbox2/usr/local/lib/X11/mini-icons ] && FNDICON="`find sandbox2/usr/local/lib/X11/mini-icons -maxdepth 1 -name $ICONBASE -o -name $ICONBASE.png -o -name $ICONBASE.xpm -o -name $ICONBASE.jpg -o -name $ICONBASE.jpeg -o -name $ICONBASE.gif -o -name $ICONBASE.svg | grep -i -E 'png$|xpm$|jpg$|jpeg$|gif$|svg$' | head -n 1`"
    if [ "$FNDICON" ];then
     ICONNAMEONLY="${FNDICON##*/}" #basename $FNDICON
     iPTN="s%^Icon=.*%Icon=${ICONNAMEONLY}%"
     sed -i -e "$iPTN" $ONEDESKTOP
     continue
    else
     #look elsewhere, including in running puppy... 111207 fix for parole /usr/share/parole/parole.png...
     FNDICON="`find sandbox2 /usr/share/icons /usr/local/share/pixmaps /usr/share/pixmaps -name $ICONBASE -o -name $ICONBASE.png -o -name $ICONBASE.xpm -o -name $ICONBASE.jpg -o -name $ICONBASE.jpeg -o -name $ICONBASE.gif -o -name $ICONBASE.svg  | sed -e 's%sandbox2%%' | grep -i -E 'png$|xpm$|jpg$|jpeg$|gif$|svg$' | head -n 1`"
     if [ "$FNDICON" ];then
      ICONNAMEONLY="${FNDICON##*/}" #basename "$FNDICON"
      mkdir -p sandbox2/usr/share/pixmaps #120514
      ln -snf "$FNDICON" sandbox2/usr/share/pixmaps/${ICONNAMEONLY} #111207 fix path.
      iPTN="s%^Icon=.*%Icon=${ICONNAMEONLY}%"
      sed -i -e "$iPTN" $ONEDESKTOP
      continue
     fi
    fi
    #substitute a default icon...
    sed -i -e "$iPATTERN" $ONEDESKTOP
   fi
  done
  #=====================================================================

  case "$PKGNAME" in
    *"-dev_"*|*"_DEV"*) #if pkg has '-dev_' in name, copy all of it to $DEV... w018 add '_DEV'
      #debian pkgs can have doc dirs and even nls dirs.
      if [ "$NLS" = "_NULL" ] ; then
        NLS_DIRS='usr/share/locale'
        for i in $NLS_DIRS ; do [ -d sandbox2/${i} ] && rm -rf sandbox2/${i} ; done
      fi
      if [ "$DOC" = "_NULL" ] ; then
        DOC_DIRS='usr/share/doc usr/share/info usr/share/man usr/share/gtk-doc usr/doc usr/info usr/man usr/share/gnome/help'
        for i in $DOC_DIRS ; do [ -d sandbox2/${i} ] && rm -rf sandbox2/${i} ; done
      fi
      if [ ! -d ${DEV_DIR} ] ; then
        mv -f sandbox2 ${DEV_DIR}
      else
        mkdir -p ${DEV_DIR}
        cp -a --remove-destination sandbox2/* ${DEV_DIR}/
      fi
      continue
      ;;
    *"-doc_"*|*"_DOC"*)
      if [ ! -d ${DOC_DIR} ] ; then
        mv -f sandbox2 ${DOC_DIR}
      else
        mkdir -p ${DOC_DIR}
        cp -a --remove-destination sandbox2/* ${DOC_DIR}/
      fi
      continue
      ;;
    *"_NLS"*)
      if [ ! -d ${NLS_DIR} ] ; then
        mv -f sandbox2 ${NLS_DIR}
      else
        mkdir -p ${NLS_DIR}
        cp -a --remove-destination sandbox2/* ${NLS_DIR}/
      fi
      continue
      ;;
  esac

  cd sandbox2
  # directories
  find . -mindepth 1 -type d | sed -e 's|^\.\/||' | sort | \
  while read ONEDIR
  do
   if [ ! -e "$ONEDIR" ] ; then
     continue #might be already moved/deleted
   fi
   PARENTDIR="${ONEDIR%/*}"  #dirname "$ONEDIR"
   [ "$PARENTDIR" = "$ONEDIR" ] && PARENTDIR=
   case "$ONEDIR" in
     #-- NLS -- international language
     *"/locale"|*"/nls"|*"/i18n")
       if [ "$NLS" = "_NULL" ] ; then
         rm -rf ${ONEDIR}
         continue
       fi
       if [ ! -d ${NLS_DIR}/${ONEDIR} ] ; then
         mkdir -p ${NLS_DIR}/${PARENTDIR}
         mv -f ${ONEDIR} ${NLS_DIR}/${PARENTDIR}/
       else
         mkdir -p ${NLS_DIR}/${ONEDIR}
         cp -a --remove-destination ${ONEDIR}/* ${NLS_DIR}/${ONEDIR}/
         rm -rf ${ONEDIR}
       fi
       continue
       ;;
     #-- DOC -- documentation
     *"/man"|*"/doc"|*"/doc-base"|*"/docs"|*"/info"|*"/gtk-doc"|*"/faq"|*"/manual"|*"/examples"|*"/help"|*"/htdocs")
       if [ "$DOC" = "_NULL" ] ; then
         rm -rf ${ONEDIR}
         continue
       fi
       if [ ! -d ${DOC_DIR}/${ONEDIR} ] ; then
         mkdir -p ${DOC_DIR}/${PARENTDIR}
         mv -f ${ONEDIR} ${DOC_DIR}/${PARENTDIR}/
       else
         mkdir -p ${DOC_DIR}/${ONEDIR}
         cp -a --remove-destination ${ONEDIR}/* ${DOC_DIR}/${ONEDIR}/
         rm -rf ${ONEDIR}
       fi
       continue
       ;;
     #-- DEV -- development
     *"X11/config"|*"/include"|*"/pkgconfig"|*"/aclocal"|*"/cvs"*|*"/svn"*|*"/src"*|*"/gcc")
       if [ "$DEV" = "_NULL" ] ; then
         rm -rf ${ONEDIR}
         continue
       fi
       if [ ! -d ${DEV_DIR}/${ONEDIR} ] ; then
         mkdir -p ${DEV_DIR}/${PARENTDIR}
         mv ${ONEDIR} ${DEV_DIR}/${PARENTDIR}/
       else
         mkdir -p ${DEV_DIR}/${ONEDIR}
         cp -a --remove-destination ${ONEDIR}/* ${DEV_DIR}/${ONEDIR}/
         rm -rf ${ONEDIR}
       fi
       continue
       ;;
   esac
  done
  
  # will post-process this...
  mkdir -p ${EXE_DIR}/
  cp -a ./* ${EXE_DIR}/
  cd $MWD

 done <<< "${FNDIT}" #################

  #...get next pkg in same GENERICNAME
 [ "$petFLG" = "pet" ] && continue #do not do post-processing.
 [ "$MISSPOSTPROC" = "yes" ] && continue #091211

 if [ "$EXE" = "" ] ; then #EXE is not redirected to _DEV, _DOC, or _NLS
  ###POST-PROCESSING FOR NON-PET PKGS### 
  # post-process $EXE_DIR...
  mkdir -p ${EXE_DIR}
  cd ${EXE_DIR}
  #this will find all files and symlinks to files...
  find -L ./ -noleaf -type f | sed -e 's|^\.\/||' |
  while read ONEFILE
  do
   ONEBASE="${ONEFILE##*/}" #basename "$ONEFILE"
   ONEPATH="${ONEFILE%/*}" #dirname "$ONEFILE"
   [ "$ONEPATH" = "$ONEFILE" ] && ONEPATH=.
   [ "$ONEPATH" = "." ] && ONEPATH=""
   #find *.so symlink files...
   case "$ONEFILE" in *.so)
    if [ -h "$ONEFILE" ];then #-h tests for symlink
     [ "$DEV" = "_NULL" ] && { rm -f "$ONEFILE" ; continue ; }
     [ ! -d "${DEV_DIR}/${ONEPATH}" ] && mkdir -p "${DEV_DIR}/${ONEPATH}"
     mv -f "$ONEFILE" "${DEV_DIR}/${ONEPATH}/" 2>/dev/null
     continue
    fi
   esac
   #find various config files... all .a .la .m4a files
   case "$ONEFILE" in *"-config"|*"config.sh"|*"Conf.sh"|*.a|*.la|*.m4)
    [ "$DEV" = "_NULL" ] && { rm -f "$ONEFILE" ; continue ; }
    [ ! -d "${DEV_DIR}/${ONEPATH}" ] && mkdir -p "${DEV_DIR}/${ONEPATH}"
    mv -f "$ONEFILE" "$ONEFILE" "${DEV_DIR}/${ONEPATH}/" 2>/dev/null
    continue
   esac
   [ "$EXE" = "_NULL" ] && { rm -f "$ONEFILE" ; continue ; }
   #anything left over goes into the main 'executable' package...
   #strip the file... but not if a cross-build (see file WOOFMERGEVARS)
   if [ "$STRIP_BINARIES" = "yes" ];then #build.conf
    if [ "$STRIP_BIN" -a "$WOOF_HOSTARCH" = "$WOOF_TARGETARCH" ];then #needs devx.
      if [ ! -h "$ONEFILE" ];then #make sure it isn't a symlink
       FILE_INFO=$(file "$ONEFILE")
       case $FILE_INFO in *"ELF"*)
        case $FILE_INFO in
          *"shared object"*) strip --strip-debug "$ONEFILE" ;;
          *"executable"*) strip --strip-unneeded "$ONEFILE" ;;
        esac
       esac
      fi
    fi
   fi
  done
 fi

 cd $MWD
 #end of splitting.
 #--------------------------------------------

 #now do some post-processing using "template" found in 'packages-templates'...
 if [ -d packages-templates/${GENERICNAME} ];then
  echo " postprocessing with packages-templates/${GENERICNAME}"
  mkdir -p packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}
  find packages-templates/${GENERICNAME}/ -empty \( -type f -o -type l \) | \
	while read file ; do rm -fv $file ; done
  cp -a --remove-destination \
	packages-templates/${GENERICNAME}/* packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}${EXE}/
  [ -f packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}/_PRE ] && rm -f packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}/_PRE
  [ -f packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}_DEV/_PRE ] && rm -f packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}_DEV/_PRE
 fi

 #a last resort fixup, if 'FIXUPHACK' exists, execute it... 111204 DISABLE_POST_INSTALL_SCRIPT code moved up.
 if [ -f "$FIXUPHACK_FILE" ];then #111210
   if [ "$EXE" = "_DEV" ];then
    cp -f "$FIXUPHACK_FILE" packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}_DEV/FIXUPHACK
    cd packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}_DEV/
   else
    cp -f "$FIXUPHACK_FILE" packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}/FIXUPHACK
    cd packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}/
   fi
   echo " executing ${FIXUPHACK_FILE}"
   sh ./FIXUPHACK
   rm -f FIXUPHACK _PRE
   cd $MWD
 fi

 [ -d packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}_NULL ] && rm -rf packages-${DISTRO_FILE_PREFIX}/${GENERICNAME}_NULL
 [ -d /tmp/${GENERICNAME} ] && rm -rf /tmp/${GENERICNAME}

done #process next GENERICNAME.

if [ -s /tmp/2createpackages-redo ];then #091211
 for ONEREDO in `cat /tmp/2createpackages-redo`
 do
  ./2createpackages $ONEREDO
 done
fi

echo
echo "Script finished."
if [ -s ERROR-2CREATEPACKAGES ];then
 echo "WARNING: Errors were logged to file ERROR-2CREATEPACKAGES"
fi

[ "$WOOF_GUI" ] && { echo -n "Press ENTER key to exit: " ; read zzz ; }

###END###
